﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BeanOverflow.Models;

namespace BeanOverflow.Controllers
{ 
    public class QuestionController : Controller
    {
        private QuestionDBContext db = new QuestionDBContext();
       

       
        // GET: /Question/
        /// <summary>
        /// Displays the existing questions in tabular format
        /// </summary>
        /// <returns>a ViewResult containing all the questions
        /// </returns>
        [Authorize]
        public ViewResult Index()
        {
            return View(db.Questions.ToList());
        }

        //
        // GET: /Question/Details/5
        /// <summary>
        /// Displays the details of the question corresponding to id
        /// </summary>
        /// <param name="id">ID of the question of interest</param>
        /// <returns>a ViewResult containing the question corresponding to id
        /// </returns>
        [Authorize]
        public ViewResult Details(int id)
        {
            Question question = db.Questions.Find(id);
            return View(question);
        }
       
        
        // GET: /Question/Create
        /// <summary>
        /// Displays an empty form where the user can provide input to the various fields
        /// </summary>
        /// <returns>an empty ActionResult</returns>
        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        
        // POST: /Question/Create
        /// <summary>
        /// Receives newly created Question and stores it on the DB
        /// </summary>
        /// <param name="question">a newly created Question</param>
        /// <returns>an ActionResult view </returns>
        [HttpPost, Authorize]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(question);
        }


       
        // GET: /Question/Ask
        /// <summary>
        /// Displays an empty form where the user can provide input to the various fields
        /// </summary>
        /// <returns>an empty ActionResult</returns>
        [Authorize]
        public ActionResult Ask()
        {
            return View();
        }

        
        // POST: /Question/Create
        /// <summary>
        /// Receives newly created Question and stores it on the DB
        /// </summary>
        /// <param name="question">a newly created Question</param>
        /// <returns>an ActionResult view </returns>
        [HttpPost, Authorize]
        public ActionResult Ask(Question question)
        {
            question.CreatedDate = DateTime.Now;
            question.LastModifiedDate = DateTime.Now;
            question.Votes = 0;
            //TODO how to get current user ID
            question.UserName = User.Identity.Name;
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("../QuestionAndResponsesModel/Details/" + question.ID);
            }

            return View(question);
        }
        
        // GET: /Question/Edit/5
        /// <summary>
        /// Displays a form where the user can provide modified the various fields of the question corresponding to id
        /// </summary>
        /// <returns>an ActionResult</returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            Question question = db.Questions.Find(id);
            return View(question);
        }

        //
        // POST: /Question/Edit/5
        /// <summary>
        /// Receives modified Question and stores it on the DB
        /// </summary>
        /// <param name="question">a recently modified Question</param>
        /// <returns>an ActionResult view </returns>
        [Authorize]
        [HttpPost]
        public ActionResult Edit(Question question)
        {
            question.LastModifiedDate = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(question);
        }

        
        // GET: /Question/Delete/5
        /// <summary>
        /// Displays the details of the question to be deleted identified by id
        /// </summary>
        /// <param name="id">id of the question to be deleted</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            Question question = db.Questions.Find(id);
            return View(question);
        }

        
        // POST: /Question/Delete/5
        /// <summary>
        /// Deletes the question identified by id
        /// </summary>
        /// <param name="id">id of the question to be deleted</param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete"), Authorize]
        public ActionResult DeleteConfirmed(int id)
        {            
            Question question = db.Questions.Find(id);
            db.Questions.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        /// <summary>
        /// Increases by one the vote count of the question identified by id
        /// </summary>
        /// <param name="id">id of the question which count will be increased</param>
        /// <returns></returns>
        [HttpPost, ActionName("VoteUp"), Authorize]
        public ActionResult VoteUp(int id)
        {
            Question question = db.Questions.Find(id);
            question.Votes = question.Votes + 1;
            int votes = question.Votes;
            db.Entry(question).State = EntityState.Modified;
            db.SaveChanges();
            return Json(votes);
        }
        /// <summary>
        /// Decreases by one the vote count of the question identified by id
        /// </summary>
        /// <param name="id">id of the question which count will be decreased</param>
        /// <returns></returns>
        [HttpPost, ActionName("VoteDown"), Authorize]
        public ActionResult VoteDown(int id)
        {
            Question question = db.Questions.Find(id);
            question.Votes = question.Votes - 1;
            int votes = question.Votes;
            db.Entry(question).State = EntityState.Modified;
            db.SaveChanges();
            return Json(votes);
        }

       /// <summary>
       /// Performs a search on the question titles
       /// </summary>
       /// <param name="searchString"> string specifieng search criteria</param>
       /// <returns>an ActionResult object which contains all the questions which match the serarch criteria
       /// </returns>
        [Authorize]
        public ActionResult SearchIndex(string searchString)
        {
            var questions = from m in db.Questions
                         select m;

            if (!String.IsNullOrEmpty(searchString))
            {
                questions = questions.Where(s => s.Title.Contains(searchString));
            }

            return View(questions);
        }

        //experimental view
        [Authorize]
        public ActionResult UnansweredIndex()
        {
            return View(db.Questions.ToList());
        }

        //experimental view  
        [Authorize]
        public ActionResult RecentIndex()
        {
             return View(db.Questions.ToList());
        }


        [Authorize]
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}